package paneles;

import java.awt.Frame;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

import java.awt.GridBagLayout;

import javax.swing.JLabel;

import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JTextField;
import javax.swing.ImageIcon;
import java.awt.SystemColor;

class EscapeDialog extends JDialog {

  private static final long serialVersionUID = 2186008034693134359L;
  private final JLabel label = new JLabel("New label");
  private final JLabel lblNewLabel = new JLabel("New label");
  private final JTextField textField = new JTextField();
  private final JTextField textField_1 = new JTextField();
  private final JButton btnHolahola = new JButton("");

public EscapeDialog() {
    super((Frame) null, false);
    textField_1.setColumns(10);
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
    gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    getContentPane().setLayout(gridBagLayout);
    
    GridBagConstraints gbc_label = new GridBagConstraints();
    gbc_label.insets = new Insets(0, 0, 5, 5);
    gbc_label.gridx = 3;
    gbc_label.gridy = 4;
    getContentPane().add(label, gbc_label);
    textField.setColumns(10);
    
    GridBagConstraints gbc_textField = new GridBagConstraints();
    gbc_textField.insets = new Insets(0, 0, 5, 5);
    gbc_textField.fill = GridBagConstraints.HORIZONTAL;
    gbc_textField.gridx = 5;
    gbc_textField.gridy = 4;
    getContentPane().add(textField, gbc_textField);
    
    GridBagConstraints gbc_btnHolahola = new GridBagConstraints();
    gbc_btnHolahola.insets = new Insets(0, 0, 5, 5);
    gbc_btnHolahola.gridx = 7;
    gbc_btnHolahola.gridy = 4;
    btnHolahola.setBorderPainted(false);
    btnHolahola.setBackground(SystemColor.menu);
    btnHolahola.setBorder(null);
    btnHolahola.setIcon(new ImageIcon(EscapeDialog.class.getResource("/icon/close_icon.png")));
    getContentPane().add(btnHolahola, gbc_btnHolahola);
    
    GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
    gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5);
    gbc_lblNewLabel.gridx = 3;
    gbc_lblNewLabel.gridy = 6;
    getContentPane().add(lblNewLabel, gbc_lblNewLabel);
    
    GridBagConstraints gbc_textField_1 = new GridBagConstraints();
    gbc_textField_1.insets = new Insets(0, 0, 0, 5);
    gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
    gbc_textField_1.gridx = 5;
    gbc_textField_1.gridy = 6;
    getContentPane().add(textField_1, gbc_textField_1);
  }

  protected JRootPane createRootPane() {
    JRootPane rootPane = new JRootPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
    Action actionListener = new AbstractAction() {

		private static final long serialVersionUID = -8862355086202525814L;

	public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("about to disappear");
        setVisible(false);
      }
    };
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "ESCAPE");
    rootPane.getActionMap().put("ESCAPE", actionListener);

    return rootPane;
  }

  public static void main(String[] a) {
    EscapeDialog dlg = new EscapeDialog();
    dlg.setUndecorated(true);
    dlg.getRootPane().setOpaque(false);
    //dlg.getContentPane().add(new JButton("asdf"));
    dlg.setSize(300, 100);
    dlg.setVisible(true);
  }
  
}
